package com.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.entity.*;
import com.project.service.*;
import com.project.util.PageBeans;
import com.project.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资产设备台账盘点控制器
 *
 * @author Leo高洋
 * @create 2023-04-23 16:04
 */
@RestController
@RequestMapping("/assetCheck")
@Slf4j
public class AssetCheckController {
    @Autowired
    private AssetService assetService;

    @Autowired
    private AssetAllocateService assetAllocateService;

    @Autowired
    private AssetFixService assetFixService;

    @Autowired
    private AssetDepreciationService assetDepreciationService;

    @Autowired
    private MechanismService mechanismService;

    @Autowired
    private StaffService staffService;

    @Autowired
    private AssetTypeService assetTypeService;

    /**
     * 资产设备台账盘点
     *
     * @param pageBeans
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody PageBeans pageBeans) {
        log.info("资产设备台账盘点");
        // 设置分页条件
        Map<String, Object> map = pageBeans.getMap();
        map.put("start", pageBeans.getStart());
        map.put("pageSize", pageBeans.getPageSize());
        // 资产信息集合
        List<Asset> assetList = assetService.list(map);
        // 资产信息总数
        long total = assetService.getTotal(map);
        for (Asset asset : assetList) {
            // 根据资产id查询资产调拨信息
            AssetAllocate assetAllocate = assetAllocateService.getOne(new QueryWrapper<AssetAllocate>().eq("assetId", asset.getId()));
            if (assetAllocate != null) {
                // 根据调拨信息中的机构id查询机构数据
                Mechanism mechanism = mechanismService.findById(String.valueOf(assetAllocate.getMechanismid()));
                // 设置机构数据
                assetAllocate.setMechanism(mechanism);
                // 根据调拨信息中的员工id查询员工数据
                Staff staff = staffService.getOne(new QueryWrapper<Staff>().eq("id", assetAllocate.getStaffid()));
                // 设置员工数据
                assetAllocate.setStaff2(staff);
                // 在资产信息中设置机构id
                asset.setMec_id(assetAllocate.getMechanismid());
            }
            // 设置资产调拨信息
            asset.setAssetAllocate(assetAllocate);
            // 资产维修
            AssetFix assetFix = assetFixService.getOne(new QueryWrapper<AssetFix>().eq("assetId", asset.getId()));
            if(assetFix != null){
                asset.setAssetFix(assetFix);
            }
            // 资产折旧
            AssetDepreciation assetDepreciation = assetDepreciationService.getOne(new QueryWrapper<AssetDepreciation>().eq("assetId", asset.getId()));
            asset.setAssetDepreciation(assetDepreciation);
            if (assetDepreciation == null) {
                asset.setAd_id(0);
            } else {
                asset.setAd_id(assetDepreciation.getId());
            }
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("assetList", assetList);
        resultMap.put("total", total);
        return R.ok(resultMap);
    }

    /**
     * 根据单位机构查询资产设备台账
     *
     * @param id
     * @return
     */
    @PostMapping("/listByMec/{id}")
    public R listByMec(@PathVariable(value = "id") Integer id) {
        log.info("根据单位机构查询资产设备台账");
        List<AssetAllocate> assetAllocateList = assetAllocateService.list(new QueryWrapper<AssetAllocate>().eq("mechanismId", id));
        List<Asset> assetList = new ArrayList<>();
        for (AssetAllocate assetAllocate : assetAllocateList) {
            Mechanism mechanism = mechanismService.findById(String.valueOf(assetAllocate.getMechanismid()));
            assetAllocate.setMechanism(mechanism);
            Staff staff = staffService.getOne(new QueryWrapper<Staff>().eq("id", assetAllocate.getStaffid()));
            assetAllocate.setStaff2(staff);
            Asset asset = assetService.getById(assetAllocate.getAssetid());
            if (asset != null) {
                List<AssetType> assetType = assetTypeService.list(new QueryWrapper<AssetType>().eq("id", asset.getTypeId()));
                asset.setAssetType(assetType);
                asset.setAssetAllocate(assetAllocate);
                // 资产折旧
                AssetDepreciation assetDepreciation = assetDepreciationService.getOne(new QueryWrapper<AssetDepreciation>().eq("assetId", asset.getId()));
                asset.setAssetDepreciation(assetDepreciation);
                if (assetDepreciation == null) {
                    asset.setAd_id(0);
                } else {
                    asset.setAd_id(assetDepreciation.getId());
                }
                // 资产维修
                AssetFix assetFix = assetFixService.getOne(new QueryWrapper<AssetFix>().eq("assetId", asset.getId()));
                if(assetFix != null){
                    asset.setAssetFix(assetFix);
                }
                assetList.add(asset);
            }
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("assetList", assetList);
        return R.ok(resultMap);
    }

    /**
     * 资产盘点详情
     *
     * @param id
     * @return
     */
    @PostMapping("/details/{id}")
    public R details(@PathVariable(value = "id") Integer id) {
        log.info("资产盘点详情");
        // 资产调拨
        List<AssetAllocate> assetAllocateList = assetAllocateService.list(new QueryWrapper<AssetAllocate>().eq("assetId", id));
        for (AssetAllocate assetAllocate : assetAllocateList) {
            Mechanism mechanism = mechanismService.findById(String.valueOf(assetAllocate.getMechanismid()));
            assetAllocate.setMechanism(mechanism);
            Staff staff = staffService.getOne(new QueryWrapper<Staff>().eq("id", assetAllocate.getStaffid()));
            assetAllocate.setStaff2(staff);
        }
        // 资产维修报废
        List<AssetFix> assetFixList = assetFixService.list(new QueryWrapper<AssetFix>().eq("assetId", id));
        // 资产折旧
        List<AssetDepreciation> assetDepreciationList = assetDepreciationService.list(new QueryWrapper<AssetDepreciation>().eq("assetId", id));
        for (AssetDepreciation assetDepreciation : assetDepreciationList) {
            AssetType assetType = assetTypeService.findById(String.valueOf(assetDepreciation.getTypeid()));
            assetDepreciation.setAssetType(assetType);
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("assetAllocateList", assetAllocateList);
        resultMap.put("assetFixList", assetFixList);
        resultMap.put("assetDepreciationList", assetDepreciationList);
        return R.ok(resultMap);
    }
}
